package gnss

import (
	"dgo/goutils/utils"
	"math"
)

func EncodeIgsSSR4(head *IgsSSRHead, lst *IgsSSR_IGM03_List, bb *utils.BytesBuilder, startByteN int) int {
	satn := uint32(0)
	for i := 0; i < len(lst); i++ {
		if lst[i].SatId > 0 {
			satn++
		}
	}
	var startBitN int = 0
	head.MsgID = 4076
	if head.NaviSys == SYS_GPS {
		startBitN = head.EncodeSSR4Head(bb, startByteN, 23, satn)
	} else if head.NaviSys == SYS_GLO {
		startBitN = head.EncodeSSR4Head(bb, startByteN, 43, satn)
	} else if head.NaviSys == SYS_BD {
		startBitN = head.EncodeSSR4Head(bb, startByteN, 103, satn)
	} else if head.NaviSys == SYS_GAL {
		startBitN = head.EncodeSSR4Head(bb, startByteN, 63, satn)
	} else if head.NaviSys == SYS_QZS {
		startBitN = head.EncodeSSR4Head(bb, startByteN, 83, satn)
	}
	startBitN = EncodeSSR4Sat(lst, bb, startByteN, startBitN)
	byteN := int(math.Ceil(float64(startBitN) / 8))
	bb.Delete2End(startByteN + byteN)
	return byteN
}

func EncodeRtcmIgsSSR4(head *IgsSSRHead, lst *IgsSSR_IGM03_List, bb *utils.BytesBuilder) {
	idx0 := bb.Len()
	bb.Write([]byte{0xD3, 0, 0})
	ByteN := EncodeIgsSSR4(head, lst, bb, bb.Len())
	bb.SetBitU16(idx0, 8+6, 10, uint16(ByteN))

	bb.CheckSize(bb.Len(), 3)
	crcBuf := bb.Buff(idx0, ByteN+3) // 3:同步标记 + 保留位 + 长度（RTCM消息体）(10bit)
	crc1 := utils.CRC24QBuf(crcBuf)
	bb.SetBitU32(idx0+3+ByteN, 0, 24, uint32(crc1))
	bb.Delete2End(idx0 + 3 + ByteN + 3)
}
